package ui
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 *菜单类,垂直排列 
	 * @author 张进辉
	 * 
	 */	
	public class MMenu extends Sprite
	{
		private static var _instance:MMenu;
		
		private static var _callback:Function;
		
		private static var _signal:Boolean = false;
		
		/**当前是否显示*/
		private static var isShow:Boolean = false;
		
		
		/**菜单显示层*/
		public static var layer:Sprite;
		
		/**
		 * 初始化菜单 
		 * @param background: 菜单背景
		 * @param mw
		 * @param fontSize
		 * 
		 */		
		public static function initMenu(background:BitmapData, fontSize:int=12):void
		{
			if(_instance)
				destroy();
			_signal = true;
			_instance = new MMenu(background,fontSize);
			_signal = false;
		}
		/**
		 *设定菜单数据,必须在initMenu之后调用,如果需要更换菜单数据,需要重新调用该方法,不更改其他属性
		 * @param data
		 * 
		 */		
		public static function setData(data:Array):void
		{
			_instance.data = data;
		}
		/**销毁菜单,调用此方法后需要重新调用initMenu方能使用menu*/
		public static function destroy():void
		{
			_instance = null;
		}
		/**
		 *显示菜单 ,调用此方法前,要保证initMenu方法已经被调用,否则将无法创建菜单
		 * @param data: 菜单数据,MenuVO对象,如果该值为null,则继续使用上次数据
		 * @param clickCallback: 点击菜单的回调,该方法返回一个MenuVO对象
		 * 
		 */		
		public static function showMenu(mx:Number,my:Number,clickCallback:Function,data:Array=null):void
		{
			if(isShow)
				_instance.clear();
				
			_callback = clickCallback;
			_instance.x = mx - 5;
			_instance.y = my - 5;
			if(data)
				_instance.data = data;
			
			layer.addChild(_instance);
			
			_instance.initEventListener();
			
			isShow = true;
		}
		
		
		
		private var _bg:Bitmap;
		
		private var _fontSize:int;
		
		/**当前选中的菜单层级*/
		private var _curLevel:int = 1;
		
		/**当前选中的菜单id*/
		private var _selectItem:MenuItem;
		/**内容*/
		private var _content:Sprite;
		/**偏移*/
		private var _offX:Number = 8;
		private var _offY:Number = 8;
		
		/**是否可关闭*/
		private var _closeEnabled:Boolean = false;
		/**
		 * 
		 * @param background: 菜单背景
		 * @param fontSize: 文本大小
		 * 
		 */		
		public function MMenu(background:BitmapData,fontSize:int=12)
		{
			super();
			
			if(!_signal) throw new Error("不能这样使用"); 
			
			_fontSize = fontSize;
			
			_bg = new Bitmap(background);
			addChild(_bg);
			
			_content = new Sprite();
			addChild(_content);
		}
		
		public function initEventListener():void
		{
			this.addEventListener(MouseEvent.MOUSE_OVER,overHandler);
			layer.stage.addEventListener(MouseEvent.CLICK,stageClickHandler);
			this.addEventListener(MouseEvent.MOUSE_OUT,outHandler);
		}
		
		protected function outHandler(event:MouseEvent):void
		{
			_closeEnabled = true;
		}
		
		protected function stageClickHandler(event:MouseEvent):void
		{
			if(_closeEnabled)
				clear();
		}		
		
		protected function overHandler(event:MouseEvent):void
		{
			_closeEnabled = false;
			var item:MenuItem = event.target as MenuItem;
			if(item && item.data.enabled)
			{
				if(_selectItem)
				{
					_selectItem.selected = false;
					_selectItem.removeEventListener(MouseEvent.CLICK,clickItemHandler);
					_selectItem = null;
				}
				
				_selectItem = item;
				_selectItem.selected = true;
				_selectItem.addEventListener(MouseEvent.CLICK,clickItemHandler);
				
			}
		}
		/**点击菜单项*/
		protected function clickItemHandler(event:Event):void
		{
			if(_selectItem.data.enabled)
				_callback(_selectItem.data);
			else
				return;
			
			clear();
		}
		
		/**
		 * 
		 * @param value
		 * 
		 */		
		public function set data(value:Array):void
		{
			_content.removeChildren();
			
			var cx:Number = _offX;
			var cy:Number = _offY;
			
			for each (var md:MenuVO in value) 
			{
				var item:MenuItem = new MenuItem(md,_fontSize);
				item.x = cx;
				item.y = cy;
				_content.addChild(item);
				cx += 0;
				cy += item.height;
			}
			
			var ch:Number = _content.height;
			var cw:Number = _content.width;
			_bg.height = ch + _offY*2;
			_bg.width = cw + _offX*2;
			
			
		}
		/**清理*/
		public function clear():void
		{
			_closeEnabled = false;
			isShow = false;
			
			this.removeEventListener(MouseEvent.MOUSE_OVER,overHandler);
			layer.stage.removeEventListener(MouseEvent.CLICK,stageClickHandler);
			this.removeEventListener(MouseEvent.MOUSE_OUT,outHandler);
			
			if(_selectItem)
			{
				_selectItem.selected = false;
				_selectItem.removeEventListener(MouseEvent.CLICK,clickItemHandler);
				_selectItem = null;
			}
			
			layer.removeChild(_instance);
			
		}
		
		
		
	}
}


import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;

import ui.MenuVO;


class MenuItem extends Sprite
{
	private var _text:TextField;
	
	private var _data:MenuVO;
	
	private var _sl:Sprite;
	
	private var _fontSize:int;
	
	public function MenuItem(data:MenuVO,fontSize:int)
	{
		super();
		
		_data = data;
		
		_fontSize = fontSize;
		
		_sl = new Sprite();
		addChild(_sl);
		
		_text = new TextField();
		_text.mouseEnabled = false;
		_text.height = fontSize+10;
		_text.text = _data.label;
		addChild(_text);
		
		this.graphics.beginFill(0,0);
		this.graphics.drawRect(0,0,_text.width,_text.height);
		this.graphics.endFill();
	}
	
	/**设定选中*/
	public function set selected(value:Boolean):void
	{
		if(_data.enabled)
		{
			if(value)
			{
				_sl.graphics.beginFill(0,0.8);
				_sl.graphics.drawRect(0,0,_text.width,_text.height);
				_sl.graphics.endFill();
				
				_text.defaultTextFormat = new TextFormat(null,_fontSize,0xef8d00);
				_text.text = _data.label;
			}
			else
			{
				_sl.graphics.clear();
				_text.defaultTextFormat = new TextFormat(null,_fontSize,0xffffff);
				_text.text = _data.label;
			}
		}
	}
	
	/**数据*/
	public function get data():MenuVO
	{
		return _data;
	}
}